package cn.th.controller;


import cn.th.common.R;
import cn.th.dto.AdDto;
import cn.th.dto.RouteDto;
import cn.th.dto.UserDto;
import cn.th.entity.AdvLabel;
import cn.th.entity.User;
import cn.th.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author cn.th
 * @since 2024-03-30
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 查找用户
     * @param page
     * @param limit
     * @param school 所在学校
     * @param name 该生姓名，不支持模糊查询
     * @param sex 性别
     * @param myClass 班级
     * @param sno 学号
     * @param location 家乡
     * @param major 专业
     * @param academy 学院
     * @param dormitory 宿舍楼
     * @param dormitoryNumber 宿舍号
     * @param state 状态
     * @return
     */
    @GetMapping("/list")
    public R<Map<String,Object>> list(@RequestParam(value = "page") int page,
                                      @RequestParam(value = "limit") int limit,
                                      @RequestParam(value = "school", required = false) String school,
                                      @RequestParam(value = "name", required = false) String name,
                                      @RequestParam(value = "sex", required = false) String sex,
                                      @RequestParam(value = "myClass", required = false) String myClass,
                                      @RequestParam(value = "sno", required = false) String sno,
                                      @RequestParam(value = "location", required = false) String location,
                                      @RequestParam(value = "major", required = false) String major,
                                      @RequestParam(value = "academy", required = false) String academy,
                                      @RequestParam(value = "dormitory", required = false) String dormitory,
                                      @RequestParam(value = "dormitoryNumber", required = false) String dormitoryNumber,
                                      @RequestParam(value = "state", required = false) String state){

        Map<String,Object> data = userService.queryUser(page, limit, school, name, sex, myClass, sno, location, major, academy, dormitory, dormitoryNumber, state);
        return R.success(data);
    }

    /**
     * 新增用户，采用文件导入
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R<String> uploadExcel(@RequestParam("file") MultipartFile file) {
        userService.saveFile(file);
        return R.success("新增用户成功！！！");
    }

    /**
     * 删除用户
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(String ids){
        log.info("删除用户信息，ids为：{}",ids);

        //删除用户表的数据
        userService.deleteById(ids);

        return R.success("删除用户成功");
    }

    /**
     * 修改用户信息
     * @param userDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody UserDto userDto){
        log.info("修改用户信息：{}",userDto);

        userService.updateWithId(userDto);
        return R.success("修改用户成功");
    }

    /**
     * 修改用户时页面的数据展示
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<UserDto> getUserDtoById(@PathVariable("id")Long id){
        UserDto userDto = userService.getUserDtoById(id);
        return R.success(userDto);
    }
}

